package com.freesky.biz.psbc.service.impl;

import com.freesky.biz.psbc.domain.TPsbcMarket;
import com.freesky.biz.psbc.domain.TPsbcMarketBankCount;
import com.freesky.biz.psbc.domain.TPsbcMarketParkCount;
import com.freesky.biz.psbc.mapper.PsbcMarketMapper;
import com.freesky.biz.psbc.service.IPsbcMarketService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PsbcMarketServiceImpl implements IPsbcMarketService {

    @Autowired
    private PsbcMarketMapper psbcMarketMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TPsbcMarket> findList(TPsbcMarket entity) {
        List<TPsbcMarket> psbcMarketList = psbcMarketMapper.findList(entity);
        for(int i=0;i<psbcMarketList.size();i++){
            TPsbcMarket psbcMarket = psbcMarketList.get(i);
            String companyLable = psbcMarket.getCompanyLabel();
            if(StringUtils.isNotEmpty(companyLable)){
                String []arrCompany = companyLable.split(",");
                String companyMsg = "";
                //企业标签：1、推荐，2、专精特精型，3、创新型
                for(int j=0;j<arrCompany.length;j++){
                    if("1".equalsIgnoreCase(arrCompany[j])){
                        companyMsg = companyMsg + "推荐,";
                    } else if("2".equalsIgnoreCase(arrCompany[j])){
                        companyMsg = companyMsg + "专精特精型,";
                    } else if("3".equalsIgnoreCase(arrCompany[j])){
                        companyMsg = companyMsg + "创新型,";
                    }
                }
                if(StringUtils.isNotEmpty(companyMsg)){
                    psbcMarket.setCompanyLabel(companyMsg.substring(0,companyMsg.length()-1));
                }
            }

            String peopleNum = psbcMarket.getPeopleNum();
            if(StringUtils.isNotEmpty(peopleNum)){
                if("1".equalsIgnoreCase(peopleNum)){
                    psbcMarket.setPeopleNum("<0-20人");
                } else if("2".equalsIgnoreCase(peopleNum)){
                    psbcMarket.setPeopleNum("20-99人");
                } else if("3".equalsIgnoreCase(peopleNum)){
                    psbcMarket.setPeopleNum("100-499人");
                } else if("4".equalsIgnoreCase(peopleNum)){
                    psbcMarket.setPeopleNum("500-999人");
                } else if("5".equalsIgnoreCase(peopleNum)){
                    psbcMarket.setPeopleNum("1000-9999人");
                } else if("6".equalsIgnoreCase(peopleNum)){
                    psbcMarket.setPeopleNum("1万人以上");
                }
            }
            psbcMarketList.set(i,psbcMarket);
        }
        return psbcMarketList;
    }

    @Override
    public AjaxResult exportFile(TPsbcMarket entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("PSBC_MARKET");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TPsbcMarket(), IPsbcMarketService.class,
                "findList", "营销地图",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }


    @Override
    public List<TPsbcMarketBankCount> findBankCountList(TPsbcMarketBankCount entity) {
        return psbcMarketMapper.findBankCountList(entity);
    }

    @Override
    public AjaxResult exportBankCountFile(TPsbcMarketBankCount entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("PSBC_MARKET_BANK");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TPsbcMarketBankCount(), IPsbcMarketService.class,
                "findBankCountList", "支行人员走访情况统计表",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public List<TPsbcMarketBankCount> findParkCountByBankId(TPsbcMarketBankCount entity) {
        return psbcMarketMapper.findParkCountByBankId(entity);
    }

    @Override
    public List<TPsbcMarketParkCount> findParkCountList(TPsbcMarketParkCount entity) {
        return psbcMarketMapper.findParkCountList(entity);
    }

    @Override
    public AjaxResult exportParkCountFile(TPsbcMarketParkCount entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("PSBC_MARKET_PARK");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TPsbcMarketParkCount(), IPsbcMarketService.class,
                "findParkCountList", "企业园区情况统计表",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

}
